<?php
namespace app\admin\controller;

use think\Controller;

class Admin extends Base
{
    public function add()
    {
//        $this->model = 'AdminUser';
        if(request()->isPost()){
            $data = input('post.');
//            halt($data);
            $validate = validate('AdminUser');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            //判断用户名是否存在
            $username = model('AdminUser')->get(['username'=>$data['username']]);
            if ($username){
                $this->error('该用户名已存在，请更换其它名称！');
            }
            $data['password'] = md5($data['password'].'_#sing_ty');
            $data['status'] = 1;
            try{
                $id = model('AdminUser')->add($data);
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }

            if ($id){
                $this->success('id='.$id.'的用户新增成功');
            }else{
                $this->error('新增失败');
            }
        }else{
            return $this->fetch();
        }
    }

}
